/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller <philipp.mueller@gmx.de>         *
 *   Copyright (C) 2008 by Tanguy Krotoff <tkrotoff@gmail.com>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtGui>

#include "qtvomp.h"
#include "widgetosd.h"

WidgetOsd::WidgetOsd( MainWindow * mainWindow, QWidget *parent ) : QWidget( parent ),
    m_mainWindow( mainWindow )
{
    m_osdTv = new WidgetOsdTv( m_mainWindow, this );
    m_osdTv->hide();

    m_osdRecording = new WidgetOsdRecording( this );
    m_osdRecording->hide();

    QVBoxLayout* vbox  = new QVBoxLayout();
    vbox->setContentsMargins( 0, 0, 0, 0 ); //We don't want margins, this widget is just a placeholder

    vbox->addWidget( m_osdTv );
    vbox->addWidget( m_osdRecording );

    setLayout( vbox );

    //We always want 5s display time and need the hiding only once
    m_timer.setSingleShot( true );
    m_timer.setInterval( 5000 );

    connect( &m_timer, SIGNAL( timeout() ),
             this,     SLOT(   hide() ) );

    connect( this,     SIGNAL( dataChanged( const QModelIndex & ) ),
             m_osdTv,  SLOT(   updateOsdTv( const QModelIndex & ) ) );
}


WidgetOsd::~WidgetOsd()
{
    m_timer.stop();
}


void WidgetOsd::toggleOsd( bool restartTimer )
{
    if ( m_osdTv->isHidden() && m_osdRecording->isHidden() )
    {
        qDebug() << "WidgetOsd::showOsd called, but no osd widget should be visible";
        return;
    }
    if ( isHidden() )
    {
        if( restartTimer )
        {
            m_timer.start();
        }
        show();
    }
    else
    {
        m_timer.stop();
        hide();
    }
}


void WidgetOsd::showOsdTv( const QModelIndex &index )
{
    m_osdTv->setIndex( index );

    m_osdRecording->hide();
    m_osdTv->show();

    m_timer.start();
//     QTimer::singleShot( 5000, this, SLOT( hide() ) );

    centerWidget();
}


void WidgetOsd::showOsdRecording( const RecordingsWidgetItem* item )
{
    m_osdRecording->setItem( item );

    m_osdTv->hide();
    m_osdRecording->show();

    m_timer.start();

    centerWidget();
}


void WidgetOsd::updateOsdTv( const QModelIndex & topLeft, const QModelIndex & /*bottomRight*/ )
{
    emit dataChanged( topLeft );
}


void WidgetOsd::centerWidget()
{
    qDebug() << "WidgetOsd::centerWidget called";

    QWidget* parent = static_cast<QWidget*>( this->parent() ); // WidgetPlayVideo

    setWindowFlags( Qt::ToolTip );
    resize( parent->size().width() - 4, 56 );

    QPoint pos = parent->pos();
    pos.setY( parent->size().height() - 2 - 56 );
    pos.setX( 2 );

    move( parent->mapToGlobal( pos ) );

    if ( m_timer.isActive() )
    {
        show();
    }
}

